package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.RolePermission;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 角色权限表dao
 * @author Administrator
 *
 */
@SuppressWarnings("rawtypes")
public class RolePermissionDaoImp implements CommnonDao{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(Object value) throws Exception {
		//判断类   instanceof 
		RolePermission rpe=null;
		if(value instanceof RolePermission){
			rpe=(RolePermission)value;
			String sql="INSERT INTO  t_role_permission(role_name,permission_name,role_permission_state) VALUES (?,?,?)";
			long Number=JdbcUtil.insertAuto(sql,
					rpe.getRoleName(),
					rpe.getPermissionName(),
					rpe.getRolePermissionState()
					);
			return Number;
		}
		return 0;
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(Object value) throws Exception {
		RolePermission rpe=null;
		if(value instanceof RolePermission){
			rpe=(RolePermission)value;
			String sql="UPDATE  t_role_permission SET role_name=?,permission_name=?,role_permission_state=?  WHERE  role_permission_id=?";
			int Number=JdbcUtil.update(sql,
					rpe.getRoleName(),
					rpe.getPermissionName(),
					rpe.getRolePermissionState(),
					rpe.getRolePermissionId());
			return Number;
		}
		return 0;
	}
	/**
	 * 删除内容
	 * 角色信息表t_role，人物信息表t_character
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(Object value) throws Exception {
		RolePermission rpe=null;
		if(value instanceof RolePermission){
			rpe=(RolePermission)value;
			String sql="DELETE FROM t_role_permission WHERE role_name=?";
			int Number=JdbcUtil.update(sql,rpe.getRoleName());
			return Number;
		}
		return 0;
	}

	/**
	 * 查找所有的内容
	 * 角色信息表t_role，人物信息表t_character
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<RolePermission> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT * FROM t_role_permission LIMIT ?,?";
		return JdbcUtil.query(RolePermission.class,sql,(pageNo-1)*pageSize,pageSize);
	}

	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<RolePermission> findAll(Object key) throws Exception {
		String sql="SELECT * FROM t_role_permission WHERE permission_name=? OR role_name=? ";
		return JdbcUtil.query(RolePermission.class,sql, key, key);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<RolePermission> findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="SELECT * FROM t_role_permission WHERE permission_name=? OR role_name=? LIMIT ?,?";
		return JdbcUtil.query(RolePermission.class,sql, key, key,(pageNo-1)*pageSize,pageSize);
	}
	@Override
	public Object findByKey(Object key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	
	
	

}
